﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using Citi.GLCosmos.Domain;
using Citi.RoutineBase;

namespace Citi.GLCosmos.SchedulerUI
{
    public class Feed : IExecutableRoutine
    {
        public IEnumerable<RoutineParameter> Parameters { get { return null; } }

        public void Rollback(ref ICurrentAction currentAction, Dictionary<string, object> parameters)
        {

        }

        public void Start(DateTime scheduledDate, ref ICurrentAction currentAction, Dictionary<string, object> parameters)
        {
            MakeI7Feed();
        }

        public void Stop(ref ICurrentAction currentAction, Dictionary<string, object> parameters)
        {

        }

        public void MakeI7Feed()
        {
            string fileName = string.Format("GL_COSMOS_{0}.txt", DateTime.Now.ToString("yyyyMMdd"));
            var tempFile = Path.Combine(@"C:\temp", fileName);

            if (System.IO.File.Exists(tempFile))
            {
                File.SetAttributes(tempFile, FileAttributes.Normal);
                System.IO.File.Delete(tempFile);
            }

            var glInformation = GL.GenerateReport(DateTime.Today.Date, "O&T");

            using (var streamWriter = new StreamWriter(tempFile, true))
            {
                streamWriter.WriteLine(@"SUBSIDIARIA|DESCRICAO DA SUBSIDIARIA|ENTIDADE|FILIAL|SETOR|VALOR|TIPO DA TRANSACAO|DESCRICAO DO EVENTO|CODIGO DO EVENTO|NUMERO DO EVENTO|DATA DA CONTABILIDADE|MOEDA|CODIGO DA TRANSACAO|OPERADOR INPUT|OPERADOR AUTORIZADOR|MODULO|PROVA");

                foreach (var item in glInformation)
                {
                    streamWriter.WriteLine(string.Format(
                        "{0}|{1}|{2}|{3}|{4}|{5}|{6}|{7}|{8}|{9}|{10}|{11}|{12}|{13}|{14}|{15}|{16}",
                        item.SubsidiaryCode,
                        item.SubsidiaryDescription,
                        item.Entity,
                        item.Filial,
                        item.SectorCode,
                        item.Value,
                        item.TransactionType,
                        string.Concat(item.HistoricOne, " ", item.HistoricTwo),
                        string.IsNullOrWhiteSpace(item.EventCode) ? "-" : item.EventCode,
                        string.IsNullOrWhiteSpace(item.EventNumber) ? "-" : item.EventNumber,
                        item.Date != null ? item.Date.ToString("yyyyMMdd") : string.Empty,
                        item.Money.ToString("0#"),
                        item.TransactionCode,
                        item.OPInput,
                        item.OPAuth,
                        item.Module != null ? item.Module : string.Empty,
                        item.Prove != null ? item.Prove : string.Empty));
                }
            }

            string pathToSave = ConfigurationManager.AppSettings["FeedPathToSave"];

            foreach (var path in pathToSave.Split('|'))
                File.Copy(tempFile, Path.Combine(path, fileName), true);
        }
    }
}
